package com.wangsd.web.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.binarywang.wxpay.bean.coupon.WxPayCouponStockQueryRequest;
import com.github.binarywang.wxpay.bean.coupon.WxPayCouponStockQueryResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.wangsd.common.component.WxPayServer;
import com.wangsd.common.exception.BusinessException;
import com.wangsd.web.coupon.entity.BusinessCoupon;
import com.wangsd.web.coupon.entity.Company;
import com.wangsd.web.coupon.entity.Product;
import com.wangsd.web.coupon.entity.Store;
import com.wangsd.web.coupon.mapper.BusinessCouponMapper;
import com.wangsd.web.coupon.mapper.ProductMapper;
import com.wangsd.web.coupon.service.CompanyService;
import com.wangsd.web.coupon.service.ProductService;
import com.wangsd.web.coupon.service.StoreService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author generator
 * @since 2019-09-25
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Resource
    ProductMapper productMapper;

    @Autowired
    BusinessCouponMapper businessCouponMapper;

    @Autowired
    StoreService storeService;

    @Autowired
    CompanyService companyService;

    @Autowired
    WxPayServer wxPayServer;

    @Override
    @Transactional
    public void addOrUpdate(Product product) {
        if (product.getId() != null) {
            productMapper.updateById(product);
        } else {
            if (product.getType()==2&&StringUtils.isNotBlank(product.getStockId())) {
                WxPayCouponStockQueryResult result = null;
                try {
                    WxPayCouponStockQueryRequest request = WxPayCouponStockQueryRequest.newBuilder().build();
                    request.setCouponStockId(product.getStockId());
                    Company company = companyService.getById(product.getCompanyId());
                    result = wxPayServer.getWxPayService(company.getBusinessServiceId()).queryCouponStock(request);
                } catch (WxPayException e) {
                    throw new BusinessException(e.getErrCodeDes());
                }
                product.setProductCouponMininumn(result.getCouponMinimum());
                product.setProductCouponValue(result.getCouponValue());
            }
            Store store = storeService.getById(product.getStoreId());
            product.setCode(store.getCode());
            product.setUsableProductCount(product.getProductCount());
            productMapper.insert(product);
        }
    }

    @Override
    public IPage<Product> selectPage(String code, Integer pageNum, Integer pageSize, Product product) {
        Page<Product> page = new Page<>(pageNum, pageSize);
        IPage<Product> productIPage = productMapper.selectListXml(page, code, product);
        return productIPage;
    }

    @Override
    public Product selectRepeat(String name, String id) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        if (id != null) {
            queryWrapper.ne("id", id);
        }
        return productMapper.selectOne(queryWrapper);
    }

    @Override
    public int updateState(Product product) {
        int i = productMapper.updateById(product);
        return i;
    }

    @Override
    public List<Product> selectProductListByCompanyId(Product product) {
        List<Product> list = productMapper.selectProductListByCompanyId(product);
        return list;
    }

    @Override
    @Transactional
    public int returnProduct(Product product) {
        if(product.getType()==1){
            Product product1 = productMapper.selectById(product.getId());
            product.setUsableProductCount(product1.getUsableProductCount() + 1);
            productMapper.updateById(product);
            return 1;
        }else if (product.getType()==30){
            BusinessCoupon businessCoupon = businessCouponMapper.selectById(product.getId());
            BusinessCoupon newBusinessCoupon = new BusinessCoupon();
            newBusinessCoupon.setUsableBusinessCouponCount(businessCoupon.getUsableBusinessCouponCount() + 1);
            newBusinessCoupon.setId(businessCoupon.getId());
            businessCouponMapper.updateById(newBusinessCoupon);
            return 1;
        }else{
            return 0;
        }

    }
}
